package com.xsh.util;


import com.xsh.config.WxConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * @author lixing
 * @date 2021/6/6 10:21
 */
@Component
public class TokenUtil {
    @Autowired
    WxConfig wxConfig;
    @Autowired
    RestTemplate restTemplate;

    //微信模板接口
    private final String SEND_TEMPLAYE_MESSAGE_URL = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";


    //获取微信ACCESS_TOKEN接口
    private final String aturl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=";

    public String getAccess_token() {
        String APPID = wxConfig.getAppID();
        String SECRET = wxConfig.getAppsecret();
        String ACCESS_TOKEN = "";
        String url = aturl + APPID + "&secret=" + SECRET;
        Map template = restTemplate.getForObject(url, Map.class);
        ACCESS_TOKEN = (String) template.get("access_token");
        return ACCESS_TOKEN;
    }

    public static void main(String[] args) {
        TokenUtil util = new TokenUtil();
        String token = util.getAccess_token();
        System.out.println(token);
    }
}
